/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.Serializable;
import java.util.Map;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
//import javax.faces.component.html.HtmlOutputText
//import javax.faces.render.

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class CompositeControlBean implements Serializable {

      private transient UIComponent panelGrid;

      public UIComponent getPanelGrid() {
        if (panelGrid == null) {
          panelGrid = createCompositePanel();
        }
        return panelGrid;
      }

      public void setPanelGrid(UIComponent panelGrid) {
        this.panelGrid = panelGrid;
      }

      private UIComponent createCompositePanel() {
        initContextMemebers();

        UIComponent commandLink = createCommandLink();
        // if backend have textbox
        UIComponent textbox = createTextbox();

        String id = view.createUniqueId();
        UIComponent panelGrid = application
            .createComponent("javax.faces.HtmlPanelGroup");
        panelGrid.setId(id);
        panelGrid.setRendererType("javax.faces.Group");

        panelGrid.getChildren().add(commandLink);
        panelGrid.getChildren().add(textbox);

        return panelGrid;
      }

      private UIComponent createCommandLink() {
        // create control
        String id = view.createUniqueId();
        UIComponent commandLink = application
            .createComponent("javax.faces.HtmlCommandLink");
        commandLink.setId(id);
        commandLink.setRendererType("javax.faces.Link");
        // set attributes (bind to printHello method)
        Map<String, Object> attributes = commandLink
            .getAttributes();
        MethodExpression action = expressionFactory
            .createMethodExpression(elContext,
                "#{compositeControlBean.printHello}",
                String.class, new Class<?>[0]);
        attributes.put("value", "print hello");
        attributes.put("actionExpression", action);
        return commandLink;
      }
      
      private UIComponent createTextbox() {
        // create control
        String id = view.createUniqueId();
        UIComponent textbox = application
            .createComponent("javax.faces.HtmlInputText");
        textbox.setId(id);
        textbox.setRendererType("javax.faces.Text");
        // set attributes (bind to printHello method)
        Map<String, Object> attributes = textbox
            .getAttributes();
        // ???
        MethodExpression action = expressionFactory.createMethodExpression(elContext,"#{compositeControlBean.printHello}",String.class, new Class<?>[0]);
        attributes.put("value", "enter name here");
        attributes.put("actionExpression", action);
        return textbox;
      }

      private transient FacesContext context;
      private transient Application application;
      private transient ELContext elContext;
      private transient ExpressionFactory expressionFactory;
      private transient UIViewRoot view;

      private void initContextMemebers() {
        context = FacesContext.getCurrentInstance();
        application = context.getApplication();
        elContext = context.getELContext();
        expressionFactory = application.getExpressionFactory();
        view = context.getViewRoot();
      }

      public String printHello() {
        System.out.println("Hello");
        return null;
      }

}
